<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>订单</title>
    <script src="../modules/js/common.js"></script>
</head>
<body>
<h1 id="h1" align="center"></h1>
<div class="caption" align="center">
    <button onclick="doGetObjects()">刷新</button>
    <button onclick="goBack()">返回首页</button>
    <table class="table-bordered">
        <thead>
        <tr>
            <td>订单编号</td>
            <td>用户ID</td>
            <td>用户名</td>
            <td>商品ID</td>
            <td>商品名称</td>
            <td>数量</td>
            <td>更新时间</td>
            <td>删除</td>
        </tr>
        </thead>
        <tbody id="tbody">
        <tr><th>数据正在加载中...</th></tr>
        </tbody>
    </table>
    <div class="pagination" ></div>
</div>
<script>
    $(function(){
        $(".pagination").load("../modules/common/page.html",doGetObjects());
        $("#h1").append($.cookie("username")+"的订单");
    })

    function doGetObjects(){
        let data={pageCurrent:window.pageCurrent,pageSize: 5};
        let username=$.cookie("username");
        let url="http://localhost:8080/order/"+username;
        $.ajax({
            url:url,data:data,
            crossDomain:true,
            xhrFields:{withCredentials:true},
            success(result){
                doInitPageInfo(result);
                console.log(result);
                $("#tbody").empty();
                result.data.list.forEach((item,i)=>
                    $("#tbody").append(
                        `<tr >
                            <td>${item.id}</td>
                            <td>${item.userId}</td>
                            <td>${item.userName}</td>
                            <td>${item.itemId}</td>
                            <td>${item.itemName}</td>
                            <td>${item.num}</td>
                            <td>${item.createdTime}</td>
                            <td><button onclick="doDeleteItem(${item.id})">删除</button></td>
                         </tr>
                         `
                    ))
            }
        })
    }


    function doDeleteItem(orderId){
        var r=confirm("确认删除该订单吗?")
        if(r==false){return;};
        $.ajax({
            method:"delete",
            url:`http://localhost:8080/order/${orderId}`,
            crossDomain: true,xhrFields: {withCredentials: true},
            success(result) {
                if(result.state==1){
                    alert("删除成功")
                }else{
                    alert("删除失败")
                }
            }
        });
        doGetObjects();
    }


    function goBack(){
        window.location.href="http://localhost:8080";
    }
</script>
</body>
</html>